package cn.com.assistant.controller;

import cn.com.assistant.annotation.AdminLoginToken;
import cn.com.assistant.entity.SynonymEntity;
import cn.com.assistant.entity.TableEntity;
import cn.com.assistant.mapper.SynonymMapper;
import cn.com.assistant.mapper.TableMapper;
import cn.com.assistant.service.SynonymService;
import cn.com.assistant.utils.RequestUtil;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/synonym")
public class SynonymController {
 
    @Autowired
    SynonymService synonymService;
    @Autowired
    SynonymMapper synonymMapper;
    @Autowired
    TableMapper tableMapper;

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/insert")
    @ApiOperation(value = "添加同义词", notes = "添加同义词")
    @ApiImplicitParam(name = "synonymEntity", value = "同义词", required = true, dataType = "SynonymEntity")
    public ResponseMessage insert(@RequestBody @Valid SynonymEntity synonymEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        synonymEntity.setId(UUIDUtil.getUUID());
        synonymEntity.setCreatedDate(new Date());
        // 存储关联实体信息
        TableEntity tableEntity = tableMapper.selectById(synonymEntity.getPrincipalId());
        synonymEntity.setPrincipalProperty(tableEntity.getChineseName());
        boolean flag = synonymService.save(synonymEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("保存成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("保存失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PutMapping("/update")
    @ApiOperation(value = "修改同义词", notes = "修改同义词")
    @ApiImplicitParam(name = "synonymEntity", value = "同义词", required = true, dataType = "SynonymEntity")
    public ResponseMessage update(@RequestBody @Valid SynonymEntity synonymEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        synonymEntity.setUpdatedDate(new Date());
        boolean flag = synonymService.updateById(synonymEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除同义词", notes = "删除同义词")
    @ApiImplicitParam(name = "同义词id", value = "同义词id")
    public ResponseMessage delete(@RequestParam("id") String id) {

        boolean flag = synonymService.removeById(id);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("删除成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("删除失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/findById")
    @ApiOperation(value = "查询同义词", notes = "查询同义词")
    @ApiImplicitParam(name = "同义词id", value = "同义词id")
    public ResponseMessage findById(@RequestParam("id") String id) {
        SynonymEntity entity = synonymService.getById(id);
        return ResponseMessage.defaultResponse().setData(entity);
    }


    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ApiOperation(value = "同义词分页列表", notes = "同义词分页列表")
    @GetMapping(value = "/page")
    public ResponseMessage findByPage(@RequestParam(value = "page", defaultValue = "0") Integer pageNum,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      HttpServletRequest request) {
        Map<String, Object> filters = RequestUtil.getParameters(request);
        LambdaQueryWrapper<SynonymEntity> synonymLambdaQueryWrapper = Wrappers.lambdaQuery();
        Page<SynonymEntity> synonymPage = new Page<>(pageNum , size , true);
        IPage<SynonymEntity> synonymIPage = synonymMapper.selectPage(synonymPage , synonymLambdaQueryWrapper);
        return ResponseMessage.defaultResponse().setData(synonymIPage);
    }


}